package com.gopay.bis.service.bank.impl.wap;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.manager.bank.Wap.TBankWapB2cManager;
import com.gopay.common.Result;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.tbank.ebank.pay.TBankMessage;
import com.gopay.common.domain.bank.tbank.ebank.query.req.TBankOrderReq;
import com.gopay.common.domain.bank.tbank.ebank.query.res.TBankOrderRes;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.WapPayInteractRemoteService;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 测试银行WapPayB2C实现
 * 
 * @TbankB2cBankServiceImpl.java
 * @author weiyajun
 */
public class TBankWapPayB2cBankServiceImpl implements WapPayInteractRemoteService {

	@Resource(name = "tbankWapB2cManager")
	private TBankWapB2cManager tbankWapB2cManager;

	/**
	 * 支付类请求
	 * 
	 * @param BaseMessage
	 * @return Result
	 */
	public Result pay(BaseMessage base) {
		Result result = new Result(false);
		TBankMessage messageB2C = new TBankMessage();
		base.setBankOrderId(base.getOrderId());
		BeanUtils.copyProperties(base, messageB2C);
		try {
			messageB2C = (TBankMessage) tbankWapB2cManager.sign(messageB2C);
			result.setDefaultModel(messageB2C);
			result.setSuccess(true);
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6029);
		}
		return result;
	}

	/**
	 * 银行返回类请求
	 * 
	 * @param BaseMessage
	 * @return Result
	 */
	@Override
	public Result bankBack(BaseMessage base) {
		Result result = new Result(false);
		try {
			TBankMessage messageB2C = new TBankMessage();
			BeanUtils.copyProperties(base, messageB2C);
			boolean flag = (Boolean) tbankWapB2cManager.verify(messageB2C);
			result.setDefaultModel(flag);
			result.setSuccess(true);
			// 验签成功入账
			if (result != null && result.isSuccess() && (Boolean) result.getDefaultModel()) {
				Map<String, String> param = new HashMap<String, String>();
				if (messageB2C.getRetCd() != null && messageB2C.getRetCd().equals("S0200")) {
					// 如果该订单成功通知后台地址入账
					param.put("orderId", messageB2C.getOrderId());
					param.put("amt", messageB2C.getAmount().toString());
					param.put("cardType", "Unknown");
					param.put("notifyNum", "1");
					param.put("payStatus", "00");
					param.put("retCode", "00");
					param.put("retMsg", messageB2C.getRetMsg());
					param.put("signData", messageB2C.getSignMessage());
					param.put("isFront", base.isFront()+"");
					result.setModel("isNotifyCashier", param);
				} else {
					// 通知后台地址该订单失败
					param.put("orderId", messageB2C.getOrderId());
					param.put("amt", messageB2C.getAmount().toString());
					param.put("cardType", "Unknown");
					param.put("notifyNum", "1");
					param.put("payStatus", "01");
					param.put("retCode", "01");
					param.put("retMsg", messageB2C.getRetMsg());
					param.put("signData", messageB2C.getSignMessage());
					param.put("isFront", base.isFront()+"");
					result.setModel("isNotifyCashier", param);
				}
			}
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6064);
		}
		return result;
	}

	/**
	 * 银行查询类请求
	 * 
	 * @param BaseMessage
	 * @return Result
	 */
	@Override
	public Result query(BaseMessage base) {
		Result result = new Result(false);
		TBankOrderReq orderReq = new TBankOrderReq();
		BeanUtils.copyProperties(base, orderReq);
		try {
			TBankOrderRes orderRes = (TBankOrderRes) tbankWapB2cManager.query(orderReq);
			result.setDefaultModel(orderRes);
			result.setSuccess(true);
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6033);
		}
		return result;
	}

	/**
	 * 银行退款类请求
	 * 
	 * @param BaseMessage
	 * @return Result
	 */
	@Override
	public Result refund(BaseMessage base) {
		return null;
	}

	/**
	 * 银行日终类请求
	 * 
	 * @param BaseMessage
	 * @return Result
	 */
	@Override
	public Result dayend(BaseMessage base) {
		return null;
	}

	@Override
	public Result queryBack(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

}
